<template>
	<view class="disease ph-64 pv-64">
		<!-- <Navigation title="宠物列表" background="#fff" /> -->
		<template v-if="buyOrder==''">
			<view class="f jc-c fs-32 fw-600 text-color4">选择医生科室</view>
			<view class="list">
				<view :class="typeId==item.value?'labelAct':'label'" v-for="(item,index) in list1" :key="index"
					@click="chooseId(item.value)">
					{{item.label}}
				</view>
			</view>
		</template>

		<template v-if="doList.length>0||buyOrder==1">
			<view class="f jc-c fs-32 fw-600 text-color4">选择宠物</view>
			<view class="box w-622 b-12 ph-28 mt-20">
				<view class="f ai-c jc-s-b pt-28 pb-25 line" v-for="(item, index) in list" :key="index">
					<view class="f ai-c">
						<image :src="item.avatar" mode="aspectFill" class="w-100 h-100 b-12 mr-20"></image>
						<view>
							<view class="fs-28 fw-600 text-color1">{{ item.name }}</view>
							<view class="fs-24 text-color3 mt-4">
								{{ item.secondCategoryName }}/{{ item.gender == 1 ? 'GG' : 'MM' }}/{{ item.age }}岁
							</view>
						</view>
					</view>
					<view class="w-120 h-56 f ai-c jc-c b-56 select fs-26 text-color" @click="toSelect(item)">选择</view>
				</view>
				<view class="f jc-c pt-20 pb-24 ai-c" @click="toAddPet">
					<image src="../../static/mine/add.png" class="w-32 h-32 mr-12"></image>
					<view class="fs-28 text-color">添加新宠物</view>
				</view>
			</view>
		</template>

	</view>
</template>
<script setup lang="ts">
	import { ref } from 'vue';
	import { onShow, onLoad } from '@dcloudio/uni-app';
	import { mypetProfile } from '@/services/api/pet';
	import { useAuthStore } from '@/store/modules/auth';
	import { orderConsult, socialUser } from '@/services/api/index/index';
	import { orderSubmit } from '@/services/api/shop/order';
	import { dictDataType, docotorList } from '@/services/api/dict';
	const authStore = useAuthStore();
	const list = ref<any[]>([]);
	const list1 = ref<any[]>([]);
	const doList = ref<any[]>([]);
	const orderData = ref({});
	const doctorId = ref('');
	const typeId = ref('');
	const openid = ref('');
	const buyOrder = ref('');
	authStore.getUserInfos();
	onLoad((opt : any) => {
		if (opt.buy_order == 1) {
			buyOrder.value = opt.buy_order
			orderData.value = {
				doctorId: opt.doctorId * 1,
				consultType: opt.consultType,
				terminal: opt.terminal,
				totalPrice: opt.totalPrice,
				petId: 0,
				orderMobile: authStore.getUserInfo.mobile,
			}
		} else {
			doctorId.value = opt.id
		}
		console.log(opt, 123123)
	});
	onShow(async () => {
		if (authStore.getOpenId) {
			openid.value = authStore.getOpenId
		} else {
			await getLogin()
		}
		await getPet();
	});
	//获取宠物
	async function getPet() {
		const { data } = await mypetProfile({});
		list.value = data;
		const res = await dictDataType({ type: 'common_doctor_dept' })
		list1.value = res.data
	}
	async function toSelect(inf : any) {
		if (doctorId.value) {
			uni.navigateTo({
				url: `/pages_serve/chat/chat?doctorId=${doctorId.value}&name=${inf.name}&avatar=${inf.avatar}&gender=${inf.gender}&age=${inf.age}&pet_id=${inf.id}`,
			});
		} else {
			orderData.value.petId = inf.id
			const { data } = await orderConsult(orderData.value);
			pay(inf, data)
		}

	}
	async function chooseId(value : string) {

		let info = {
			pageNo: 1,
			pageSize: 10,
			department: value
		}
		const { data } = await docotorList(info)
		doList.value = data.list
		if (data.list.length > 0) {
			typeId.value = value
			//随机医生匹配
			let doctor = Math.floor((Math.random() * data.list.length))
			doctorId.value = data.list[doctor].id
		} else {
			uni.showToast({
				icon: 'none',
				title: '暂未匹配到医生'
			})
		}

	}
	async function getLogin() {
		uni.login({
			provider: 'weixin',
			success: async (res) => {
				let result = await socialUser({ userID: authStore.getUserId, type: 10, state: '123', code: res.code });
				openid.value = result.data.openid
			},
		})
	}

	async function pay(inf : any, item : any) {
		console.log(orderData.value)
		const { data } = await orderSubmit({ id: item.payOrderId, channelCode: 'wx_lite', channelExtras: { openid: openid.value } });
		let displayContent = JSON.parse(data.displayContent);
		// console.log(displayContent)
		await uni.requestPayment({
			provider: "wxpay",
			timeStamp: displayContent.timeStamp,
			nonceStr: displayContent.nonceStr,
			package: displayContent.packageValue,
			signType: displayContent.signType,
			paySign: displayContent.paySign,
			success(res2) {
				uni.showToast({
					title: '支付成功',
				});
				setTimeout(() => {
					// 图文问诊
					if (orderData.value.consultType == 1) {
						uni.redirectTo({
							url: `/pages_serve/chat/chat?doctorId=${orderData.value.doctorId}&name=${inf.name}&avatar=${inf.avatar}&gender=${inf.gender}&age=${inf.age}&pet_id=${inf.id}`,
						});
					}
					// 电话问诊
					else if (orderData.value.consultType == 2) {
						uni.redirectTo({
							url:"/pages_mine/myConsultation"
						})
						
					}else{
						uni.redirectTo({
							url:"/pages_mine/myConsultation"
						})
					}
				}, 1000);

			},
			fail(e) {
				console.log(e)
				uni.showToast({
					title: '支付失败,请重新支付',
					icon: 'none'
				})
			}
		})


	}
	function toAddPet() {
		uni.navigateTo({
			url: '/pages_mine/addPet',
		});
	}
</script>
<style lang="scss" scoped>
	.disease {
		.line {
			border-bottom: 1rpx solid #dddddd;
		}

		.select {
			border: 1rpx solid #fabb3c;
		}

		.box {
			border: 1rpx solid #dddddd;
		}
	}

	.list {
		display: flex;
		flex-wrap: wrap;
		margin-top: 20rpx;

		.labelAct {
			text-align: center;
			padding: 0 24rpx;
			min-width: 152rpx;
			height: 64rpx;
			background: #FABB3C;
			border: 1px solid #FABB3C;
			color: #fff;
			border-radius: 32rpx;
			font-size: 26rpx;
			line-height: 64rpx;
			margin: 8rpx;
		}

		.label {
			text-align: center;
			padding: 0 24rpx;
			min-width: 152rpx;
			height: 64rpx;
			background: #FFFFFF;
			border: 1px solid #DDDDDD;
			border-radius: 32rpx;
			font-size: 26rpx;
			line-height: 64rpx;
			margin: 8rpx;
		}
	}
</style>